using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Doubler.Common;
using Reflector.CodeModel;
using Reflector.Framework;
using Doubler.Data;
using Doubler.Helpers;
using Doubler.Helpers.Graph;
using Reflector.CodeModel.Memory;

namespace Doubler.Templates.Recorder
{
    public class ManyRecordersTemplate : ManyTemplateBase
    {

        private List<ITypeDeclaration>  _ListOfDoubleableSubjectes;

        public List<ITypeDeclaration> ListOfDoubleableSubjectes
        {
            get
            {
                if (_ListOfDoubleableSubjectes == null)
                    _ListOfDoubleableSubjectes = new List<ITypeDeclaration>();
                return _ListOfDoubleableSubjectes;
            }
            set
            {
                _ListOfDoubleableSubjectes = value;
            }
        }

        public override string FileName
        {
            get
            {
                return string.Format(base.NameFormat, this.Subject.Name);
            }
        }

        public override string OutputFilePath
        {
            get
            {
                string NamespacePath = Regex.Replace(this.Namespace, @"\.", "/");
                return string.Format("{0}/{1}", this.OutputPath, NamespacePath);
            }
        }
                
#region Constructor

        public ManyRecordersTemplate()
            : base("", "Recorder{0}")
        {
            
        }

        public ManyRecordersTemplate(Refly.CodeDom.CodeLanguage language, string outDirectoryPath, IAssembly subject, ReflectorServices reflectorServices)
            : base(language, outDirectoryPath, subject, "Recorder{0}", reflectorServices)
        {
            if (this.Subject.Modules != null)
            {
                foreach (IModule Module in this.Subject.Modules)
                {
                    if (Module.Types != null)
                    {
                        foreach (ITypeDeclaration Possible in Module.Types)
                        {
                            if (Possible.Abstract && Reflector.Helper.IsVisible(Possible, VisibilityConfiguration.PublicConfiguration))
                            {
                                this.ListOfDoubleableSubjectes.Add(Possible);
                                this.Namespace = Regex.Match(Possible.Namespace, @"(\w+)").Captures[0].Value;
                            }
                        }
                    }
                }
            }
        }

#endregion

        public override void Generate()
        {
            foreach (ITypeDeclaration ToDouble in this.ListOfDoubleableSubjectes)
            {
                RecorderTemplate TestDoubleTemplate = new RecorderTemplate(this.OutputLanguage, this.OutputPath, ToDouble, this.Services);
                TestDoubleTemplate.Generate();
            }
        }

    }
}
